<template>
  <div id="wechat-login-box" style="display: none">
    <el-divider><span class="title">微信登录</span></el-divider>
    <div id="button-box">
      <div id="wechat-login-button" @click="loginByWechat"></div>
    </div>
  </div>
</template>

<script>
import WechatLoginTools from '../tools/WechatLoginTools'

export default {
  name: 'ThirdUserLoginBox',
  methods: {
    // 根据微信用户绑定登录
    loginByWechat: function () {
      if (localStorage.getItem('MyPrivacyAgreement') !== '1') {
        return this.$message.warning('请先勾选同意《可可商城用户隐私协议》')
      }
      let $this = this
      WechatLoginTools.getCode(function (code) {
        $this.isLoading = true
        // alert('获取到的code: '+code)
        // alert('code:' + code)
        $this.MyRequest($this.ApiDoNameMember + 'Public/loginByThirdUser', {code: code}, function (data) {
          $this.isLoading = false
          $this.CurrUser.setCache({
            token: data.token,
            username: data.username,
          })
          $this.bus.$emit('UserLoginCallBus')
          $this.$router.push('/')
        }, function (res) {
          $this.isLoading = false
          if (res.code === 2) {
            // alert('响应内容:' + JSON.stringify(res))
            $this.$message.warning('当前微信未绑定账号')
            localStorage.setItem('thirdUser', res.message)
            $this.$router.push('/WechatBinding')
          }
        })
      })
    },
  }
}
</script>

<style scoped>

</style>
